package com.hugy.test.rabbitmq04;

import com.hugy.test.basic.RabitMQConnection;
import com.hugy.test.basic.Constants;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerSmsTopic {
	private static final String SMS_QUEUE = "sms_queue_topic";

	public static void main(String[] args) throws IOException, TimeoutException {
		System.out.println("短信消费者启动");
		Connection connection = RabitMQConnection.getConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(SMS_QUEUE, false, false, false, null);

		channel.queueBind(SMS_QUEUE, Constants.TOPIC_EXCHANGE_NAME, "topic.sms");

		DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body, "UTF-8");
				System.out.println("短信消费者接收到消息" + msg);
			}
		};
		channel.basicConsume(SMS_QUEUE, true, defaultConsumer);

	}

}
